package it.koutifaris.travianmap.transformation;

public class ToroidTransformation implements XYTransformation {

	private double x0, x1;
	private double y0, y1;

	public ToroidTransformation(double x0, double x1, double y0, double y1) {
		checkBounds(x0, x1);
		checkBounds(y0, y1);

		this.x0 = x0;
		this.x1 = x1;

		this.y0 = y0;
		this.y1 = y1;
	}

	@Override
	public double transformX(double x) {
		return transform(x0, x1, x);
	}

	@Override
	public double transformY(double y) {
		return transform(y0, y1, y);
	}

	protected static void checkBounds(double x0, double x1) {
		if (x0 > x1) {
			throw new IllegalArgumentException("Invalid bounds");
		}
	}

	public static double transform(double min, double max, double x) {
		checkBounds(min, max);

		double width = max - min;
		double zeroOriginTranslation = x - min;
		double zeroOriginToroidCoords = zeroOriginTranslation % width;
		double originalOriginToroidCoors = zeroOriginToroidCoords + min;

		return originalOriginToroidCoors;
	}

	public static int transform(int min, int max, int x) {
		return (int) Math.floor(transform((double) min, (double) max, (double) x));
	}

}
